import api from "../../utils/api_util";
import baseUtil from "../../utils/base_util";
import tips from "../../utils/tip";

const app = getApp();
Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        TabCur: 0,
        winWidth: 0,
        winHeight: 0,
        scrollLeft: 0,
        statusType: [
            {
                name: "全部",
                Tabindex: 0,
                // statusText: '等待付款',
                obligationlist: []
            },
            {
                name: "待支付",
                Tabindex: 1,
                statusText: '等待付款',
                obligationlist: []
            },
            {
                name: "待发货",
                Tabindex: 2,
                statusText: '等待发货',
                obligationlist: []
            },
            {
                name: "待收货",
                statusText: '待收货',
                Tabindex: 3,
                obligationlist: [],
            },
            {
                name: "待评价",
                statusText: '评价',
                Tabindex: 4,
                obligationlist: [],
            },
            {
                name: "已完成",
                statusText: '订单完成',
                Tabindex: 5,
                obligationlist: [],
            }
        ],
        //订单列表请求参数
        status: '全部',
        pageNumber: 1,
        pageSize: 20,
        hasNext: true,
    },
    onLoad: function () {
        let line = Math.ceil(this.data.statusType[0].obligationlist.length * 440);
        this.setData({
            Llight: line
        });
    },
    onShow: function () {
        let that =this;
        this.getOrderList();
        that.data.statusType[that.data.TabCur].obligationlist = [];

        this.setData({
            pageNumber: 1,
            pageSize: 20,
            statusType:that.data.statusType,
        })
    },
    //初始化订单列表
    cleanList:function(){

    },
    /**
     * 切换状态
     */
    //滑动切换状态
    bindChange: function (e) {
        let that = this;
        let tabIndex = e.detail.current;
        let status = '';
        if (this.data.TabCur == tabIndex) {
            return false;
        } else {
            that.data.statusType.forEach(function (item, i) {
                if (i == tabIndex) {
                    status = item.name;
                }
            });

            that.data.statusType[that.data.TabCur].obligationlist = [];

            that.setData({
                TabCur: tabIndex,
                status: status,
                statusType: that.data.statusType,
                pageNumber: 1,
            });
            that.getOrderList();
        }

    },
    //点击切换状态
    tabSelect(e) {
        let that = this;

        if (this.data.TabCur == e.target.dataset.id) {
            return false;
        }
        else {
            that.data.statusType[that.data.TabCur].obligationlist = [];
            that.setData({
                TabCur: e.target.dataset.id,
                status: e.target.dataset.status,
                statusType: that.data.statusType,
                pageNumber: 1,
            })
            that.getOrderList();
        }
        console.log('dianji:', e.target.dataset.status);
        console.log('dianji-TabCur:', e.target.dataset.id);

    },
    //订单列表
    getOrderList() {
        let that = this;
        if(that.data.status == "全部"){
            that.data.status = "";
        }
        api.wxRequest.apiRequest(that, api.urls.orderList, {
            method: 'GET',
            data: {
                status: that.data.status,
                pageNumber: that.data.pageNumber,
                pageSize: that.data.pageSize,
            },
            success: function (res) {

                if (res.data.total > (res.data.size * res.data.current)) {
                    that.data.hasNext = true;
                } else {
                    that.data.hasNext = false;
                    console.log('没有了')
                }

                if (res.data.list && res.data.list.length > 0) {
                    that.data.statusType[that.data.TabCur].obligationlist = that.data.statusType[that.data.TabCur].obligationlist.concat(res.data.list);
                }

                console.log('订单列表数据:', that.data.statusType);

                that.setData({
                    statusType: that.data.statusType,
                    hasNext: that.data.hasNext,
                })
            },
        })
    },
    //确认收货
    receiptConfirm:function(e){
        let that =this;

        wx.showModal({
            title: '提示',
            content: '确定确认收货此订单吗？',
            success(res) {
                if (res.confirm) {
                    api.wxRequest.apiRequest(that, api.urls.confirmGoods, {
                        method: 'POST',
                        data: {
                            orderId:e.currentTarget.dataset.id,
                        },
                        success: function (res) {
                             tips.alert('成功确认收货~');
                        },
                    })
                } else if (res.cancel) {
                }
            }
        })

    },
    //翻页
    onReachBottom: function (e) {
        let that = this;
        if (that.data.hasNext) {
            that.data.pageNumber++;
            that.getOrderList();
        }
        that.setData({
            pageNumber: that.data.pageNumber,
        })

    },

    //判断是否能评论
    goComment:function (e) {
        let id = e.currentTarget.dataset.id;
        let that =this;
        api.wxRequest.apiRequest(that, api.urls.getApply, {
            method: 'GET',
            data: {
                orderId:id,
            },
            success: function (res) {
                baseUtil.pageJump("../Evaluation/Evaluation?orderId="+id);
            },
        })
    }
})